package com.red.dubbconsumer.service;

import com.red.dubbocommon.UserService;
import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Service
public class OrderService {
    @Resource
    private RestTemplate restTemplate;

    @DubboReference(version = "1.0")
    private UserService userService;


    public String getOrder(){
        // UNARY
//        String user = userService.getUser();
        // SERVER_STREAM
       /* userService.sayHelloServerStream("ljm", new StreamObserver<String>() {
            @Override
            public void onNext(String data) {
                System.out.println("接受到结果"+ data);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {

            }
        });*/

        // CLIENT_STREAM / BI_STREAM
        StreamObserver<String> streamObserver = userService.sayHelloStream(new StreamObserver<String>() {
            @Override
            public void onNext(String data) {

            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {

            }
        });

        streamObserver.onNext("1");
        // 做其他事情

        streamObserver.onNext("2");
        streamObserver.onNext("3");

        streamObserver.onCompleted();

        return  "succes";
  /*      String result = restTemplate.getForObject("http://localhost:8080/user", String.class);
        return result;*/
    }
}
